<?php
/**
 * Created by PhpStorm.
 * User: xuke
 * Date: 16/7/19
 * Time: 上午11:00
 */

namespace App\Services;
use Illuminate\Http\Request;
use App\Services\Curl;
use Cache;

class Wechat
{

    private $appid;
    private $appsecret;
    private $wechatToken;
    private $_requrst;
    private $access_token;

    const ACCESS_TOKEN = 'access_token';

    public function __construct(Request $request)
    {
        $this->appid = config('wechat.appid');
        $this->appsecret = config('wechat.appsecret');
        $this->wechatToken = config('wechat.wechatToken');
        $this->_requrst = $request;
    }



    public function getAuthOpenid()
    {

        if ($this->_requrst->has('code'))
        {
            $code = $this->_requrst->input('code');
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code";
            $result = Curl::curl_https($url);
            return $result;
        }else{
            return false;
        }
    }


    /**拉取用户详细信息,需用户授权
     * @param $access_token
     * @param $openid
     * @return mixed
     */
    public function getUserInfo($access_token, $openid)
    {
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
        $result = Curl::curl_https($url);
        return $result;
    }

    /**获取access_token
     * @param bool $refresh 是否强制刷新
     * @return bool
     */
    public function getAccessToken($refresh = false)
    {
        $access_token = false;
       if (!$refresh && Cache::has(self::ACCESS_TOKEN)) //首先判断缓存里面的 access_token是否存在
       {
           $access_token = Cache::get(self::ACCESS_TOKEN);
       }else{
           $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";
           $result = Curl::curl_https($url);
           if (isset($result->access_token) && !empty($result->access_token))
           {
               $access_token = $result->access_token;
               Cache::put(self::access_token(), $access_token, 110); //缓存过期时间设置成110分支
           }
       }

        $this->access_token = $access_token;
        return $this->access_token;
    }

    /**获取微信服务器ip地址
     *
     */
    public function getIpList()
    {
        $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={$this->access_token}";
        $result = Curl::curl_https($url);
        return $result;
    }


    public function getUserInfoUnionId($openid)
    {
        $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$this->access_token}&openid={$openid}&lang=zh_CN";
        $result = Curl::curl_https($url);
        return $result;
    }

    /**创建临时二维码
     * @param $sense_id  场景id
     * @return mixed
     */
    public function temporaryQR($sense_id)
    {
        $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$this->access_token}";
        $data = [
            'expire_seconds' => 604800,
            'action_name'    => 'QR_SCENE',
            'action_info'    => [
                'scene_id'   => $sense_id,
            ],
        ];
        $jsonDat = json_encode($data);
        $result = Curl::curl_https($url, $jsonDat);
        return $result;

    }


}